package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.AdminConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.ApUserAuthPageReqDTO;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserAuthMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * @author:Dikk
 * @create: 2023-08-02 09:22
 * @Description: ApUserAuthServiceImpl
 */
@Service
public class ApUserAuthServiceImpl extends ServiceImpl<ApUserAuthMapper, ApUserRealname> implements ApUserAuthService {

    @Autowired
    private ApUserAuthMapper apUserAuthMapper;

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    /**
     * 获取认证列表
     *
     * @param apUserAuthPageReqDTO
     * @return
     */
    @Override
    public ResponseResult authList(ApUserAuthPageReqDTO apUserAuthPageReqDTO) {
        //参数校验
        if (Objects.isNull(apUserAuthPageReqDTO)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        apUserAuthPageReqDTO.checkParam();

        //数据查询
        IPage ipage = new Page(apUserAuthPageReqDTO.getPage(), apUserAuthPageReqDTO.getSize());
        page(ipage, Wrappers.<ApUserRealname>lambdaQuery().eq(apUserAuthPageReqDTO.getStatus() != null,
                ApUserRealname::getStatus, apUserAuthPageReqDTO.getStatus()));

        //结果返回
        ResponseResult result = new PageResponseResult((int) ipage.getCurrent(),
                (int) ipage.getSize(), (int) ipage.getTotal());

        result.setData(ipage.getRecords());
        return result;
    }

    /**
     * 审核
     *
     * @param apUserAuthPageReqDTO
     * @return
     */
    @Override
    //TODO    seata回滚  当收到异常后抛出回滚
    public ResponseResult authPass(ApUserAuthPageReqDTO apUserAuthPageReqDTO) {
        //参数校验
        if (Objects.isNull(apUserAuthPageReqDTO)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id查询用户数据是否存在
        ApUserRealname apUserRealname = apUserAuthMapper.selectOne(Wrappers.<ApUserRealname>lambdaQuery()
                .eq(ApUserRealname::getId, apUserAuthPageReqDTO.getId()));
        if (Objects.isNull(apUserAuthPageReqDTO)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        apUserRealname.setUpdatedTime(new Date());
        //判断是通过还是驳回 根据msg是否有数据判断
        if (StringUtils.isBlank(apUserAuthPageReqDTO.getMsg())) {
            //是通过
            apUserRealname.setStatus(ApUserRealname.Status.PASS.getCode());
            apUserAuthMapper.updateById(apUserRealname);
            //查询用户信息
            ApUser apUser = apUserMapper.selectOne(Wrappers.<ApUser>lambdaQuery()
                    .eq(apUserRealname.getUserId() != null, ApUser::getId, apUserRealname.getUserId()));
            //发送信息传递数据
            kafkaTemplate.send(AdminConstants.WEMEDIAL_USER_TOPIC, JSON.toJSONString(apUser));
        } else {
            //驳回,返回状态和驳回理由
            apUserRealname.setStatus(ApUserRealname.Status.FAIL.getCode());
            apUserRealname.setReason(apUserAuthPageReqDTO.getMsg());
            apUserAuthMapper.updateById(apUserRealname);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
